package com.easy.dfcapi.res;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.common.util.IPUtil;
import com.easy.common.utils.StringUtil;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;

/**
 * Created by mac on 2018/1/3.
 */
@Component
public class Response<T> {
    private String token;
    private String data;
    private Integer timestamp;
    private String sign;

//====================================
    protected String ip;
    protected Result result  = new Result();

    @Autowired
    public void setRequest(HttpServletRequest request) {
        this.ip = IPUtil.getIpAddress(request);
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public T getData(T t) {
        if(StringUtil.isEmpty(this.data)){
            this.data="{}";
        }
        JSONObject jsonObject= JSONObject.fromObject(StringEscapeUtils.unescapeXml(this.data));
        return (T)JSONObject.toBean(jsonObject, t.getClass());
    }
    public T getData(T t, Map<String,Object> map) {
        if(StringUtil.isEmpty(this.data)){
            this.data="{}";
        }
        JSONObject jsonObject= JSONObject.fromObject(StringEscapeUtils.unescapeXml(this.data));
        return (T)JSONObject.toBean(jsonObject, t.getClass(), map);
    }
    public T getData(Class cls) {
        if(StringUtil.isEmpty(this.data)){
            this.data="{}";
        }
        JSONObject jsonObject= JSONObject.fromObject(StringEscapeUtils.unescapeXml(this.data));
        return (T)JSONObject.toBean(jsonObject, cls);
    }
    public T getData(Class cls, Map<String,Object> map) {
        if(StringUtil.isEmpty(this.data)){
            this.data="{}";
        }
        JSONObject jsonObject= JSONObject.fromObject(StringEscapeUtils.unescapeXml(this.data));
        return (T)JSONObject.toBean(jsonObject, cls, map);
    }
    public T getData() {
        if(StringUtil.isEmpty(this.data)){
            this.data = "{}";
        }
        JSONObject jsonObject= JSONObject.fromObject(StringEscapeUtils.unescapeXml(this.data));
        return (T)JSONObject.toBean(jsonObject);
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }


    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public Result getResult() {
        return result;
    }

    public void setResult(Result result) {
        this.result = result;
    }

    public Integer getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Integer timestamp) {
        this.timestamp = timestamp;
    }

    /**
     * 服务端参数有效性验证
     * @return 验证成功：返回true；严重失败：将错误信息添加到 Result 中
     */
    public boolean isValidator(){
        /*
        if(!getSign().equals(sign)){
            result.setStatus(EnumConfig.S_CODE.APIKEY_ERROR);
            return false;
        }*/
        if(timestamp!=null && timestamp>0){
            long ltime = System.currentTimeMillis()/1000 - timestamp;
            if( ltime < -3600 || ltime > 3600){
                result.setStatus(EnumConfig.S_CODE.TIMESTAMP_ERROR);
                return false;
            }
        }
        if(StringUtil.isNotEmpty(sign)){
            if(!sign.equals("6677")){
                result.setStatus(EnumConfig.S_CODE.SIGN_INVALID);
                return false;
            }
        }
        return true;
    }
}
